﻿using Framework.Core;
using Framework.ExtensionsMods.Quartz.Server;
using Framework.ExtensionsMods.Quartz.Server.IServer;
using Microsoft.Extensions.DependencyInjection;
using Quartz.Spi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static NewLife.Remoting.ApiHttpClient;

namespace Framework.ExtensionsMods.Quartz
{
	public static class TaskSchedulerExtension
	{
		public static IServiceCollection AddJobTasks(this IServiceCollection services)
		{
			//添加Quartz服务
			services.AddSingleton<IJobFactory, JobFactory>();

			services.AddTransient<ITaskSchedulerServer, TaskSchedulerServer>();

			//ITaskSchedulerServer _schedulerServer = App.GetRequiredService<ITaskSchedulerServer>();

			//var tasks = _tasksQzService.GetList(m => m.IsStart);
			//var tasks = DbScoped.SugarScope.Queryable<SysTasks>().Where(m => m.IsStart).ToList();

			////程序启动后注册所有定时任务
			//foreach (var task in tasks)
			//{
			//	var result = _schedulerServer.AddTaskScheduleAsync(task);
			//	if (result.Result.code == 200)
			//	{
			//		Console.WriteLine($"注册任务[{task.Name}]ID：{task.ID}成功");
			//		logger.Info($"注册任务[{task.Name}]ID：{task.ID}成功");
			//	}
			//}
			return services;
		}
	}
}
